
class_name CCharFunctionTimeArray
extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal MaskID_changed(old_value, new_value) 
signal FunctionID_changed(old_value, new_value) 
signal AccDay_changed(old_value, new_value) 



class SCharFunctionTime:
    var nIndex
    var tblOwner
    var m_tblAlias
    var bInited = false
    func _init(owner):
        self.UUID = 0	# 注释
        self.MaskID = owner and owner.m_UUID or 0
        self.FunctionID = 0
        self.AccDay = 0

        self.nIndex = -1
        self.tblOwner = owner
        self.m_tblAlias = {
 
        }

    func GetIndex():
        return self.nIndex

    func SetZeroValue(owner):
        self.UUID = 0	# 注释
        self.MaskID = owner and owner.m_UUID or 0
        self.FunctionID = 0
        self.AccDay = 0


    func UnPack(_bufferStream, _bOffset):
        if _bOffset :
            _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)

        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.FunctionID = _bufferStream.ReadUint16()
        self.AccDay = _bufferStream.ReadUint32()

        self.bInited = true
        if self.tblOwner :
            self.tblOwner.DispatchEvent('uuid', [self, 0, self.nIndex])

    func CopyFrom(other):
        self.nIndex = other.nIndex
        self.FunctionID = other.FunctionID
        self.AccDay = other.AccDay


    func Update(_bufferStream, _nIndex ):
        _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)
        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.FunctionID = _bufferStream.ReadUint16()
        self.AccDay = _bufferStream.ReadUint32()


    func OnDataChanged( fieldName, oldValue, newValue ):
        if oldValue == newValue or not self.tblOwner:
            return
        self.tblOwner.emit_signal(fieldName, oldValue, newValue)


    #  SCharFunctionTime Geter Setter method # 
    var UUID:
        set(_value):
            var old = self.m_UUID
            self.UUID = _value
            if self.tblOwner:
                self.tblOwner.UUID_changed(old, _value)
        get:
            return UUID

    var MaskID:
        set(_value):
            var old = self.m_MaskID
            self.MaskID = _value
            if self.tblOwner:
                self.tblOwner.MaskID_changed(old, _value)
        get:
            return MaskID

    var FunctionID:
        set(_value):
            var old = self.m_FunctionID
            self.FunctionID = _value
            if self.tblOwner:
                self.tblOwner.FunctionID_changed(old, _value)
        get:
            return FunctionID

    var AccDay:
        set(_value):
            var old = self.m_AccDay
            self.AccDay = _value
            if self.tblOwner:
                self.tblOwner.AccDay_changed(old, _value)
        get:
            return AccDay



# ================================= array ==========================
var m_UUID
var m_nCharFunctionTimeCount
var m_CharFunctionTime

signal elem_update(array, old_value, new_value, index)
signal elem_add(array, old_value, new_value, index)
signal elem_del(array, old_value, new_value, index)

func _init( _uuid ):
    self.m_UUID = _uuid
    self.m_nCharFunctionTimeCount = 0
    self.m_CharFunctionTime = []
    var nMaxIndex = self.GetCharFunctionTimeMaxCount()-1
    self.m_CharFunctionTime.resize(nMaxIndex)
    for i in range(nMaxIndex):
        self.m_CharFunctionTime[i] = SCharFunctionTime.new(self) 
        self.m_CharFunctionTime[i].nIndex = i

func UnPack(_bufferStream):
    self.m_UUID = _bufferStream.ReadInt64()
    self.m_nCharFunctionTimeCount = _bufferStream.ReadUint32()
    for i in range(self.m_nCharFunctionTimeCount):
        var elem = SCharFunctionTime.new(self)
        elem.nIndex = i
        elem.UnPack(_bufferStream, false)
        self.m_CharFunctionTime[i] = elem

func GetTableName():
    return 'CharFunctionTime'

func GetUUID():
    return self.m_UUID

func GetCharFunctionTimeCurNum():
    return self.m_nCharFunctionTimeCount

func SetCharFunctionTimeCurNum( nValue ):
    self.m_nCharFunctionTimeCount = nValue

func SetCurNum(nValue):
    self.m_nCharFunctionTimeCount = nValue

static func GetCharFunctionTimeMaxCount():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME

func GetCharFunctionTimeMaxNum():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME

func GetCharFunctionTime(_nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME:
        print("GetCharFunctionTimeEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME")
        return null

    return self.m_CharFunctionTime[_nIndex];

func SetCharFunctionTime(_Prop, _nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME:
        print("SetCharFunctionTimeEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME")
        return null
    
    var old = self.m_CharFunctionTime[_nIndex]
    var elem = SCharFunctionTime.new(self)
    elem.CopyFrom(_Prop)
    elem.nIndex = _nIndex
    self.m_CharFunctionTime[_nIndex] = elem
    elem_update.emit(self, old, elem, _nIndex)
    return elem


func AddCharFunctionTime(_Prop, _nIndex):
    var nCurNum = self.m_nCharFunctionTimeCount
    if _nIndex < 0 or _nIndex > nCurNum:
        _nIndex = nCurNum

    if not _Prop or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME:
        print("AddCharFunctionTimeEle _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME");
        return null;

    var elem = SCharFunctionTime.new(self)
    elem.CopyFrom(_Prop)
    if _nIndex < nCurNum:
        for i in range(nCurNum-1, -1, -1):
            self.m_CharFunctionTime[i] = self.m_CharFunctionTime[i - 1]
            self.m_CharFunctionTime[i].nIndex = i
    self.m_CharFunctionTime[_nIndex] = elem
    self.m_nCharFunctionTimeCount = nCurNum + 1
    elem_add.emit(self, null, elem, _nIndex)

func DelCharFunctionTime(_nIndex):
    if _nIndex < 0 or _nIndex >= self.m_nCharFunctionTimeCount:
        return
    
    self.m_nCharFunctionTimeCount = self.m_nCharFunctionTimeCount - 1
    if _nIndex != self.m_nCharFunctionTimeCount:
        self.m_CharFunctionTime[_nIndex] = self.m_CharFunctionTime[self.m_nCharFunctionTimeCount]

    # var nLastDataID = self.m_CharFunctionTime[self.m_nCharFunctionTimeCount]
    var elem = self.m_CharFunctionTime[_nIndex]
    for i in range(_nIndex + 1, self.m_nCharFunctionTimeCount - 1):
        self.m_CharFunctionTime[i - 1] = self.m_CharFunctionTime[i]
        self.m_CharFunctionTime[i - 1].nIndex = i - 1
    self.m_CharFunctionTime[self.m_nCharFunctionTimeCount - 1] = null
    elem_del.emit(self, elem, null, _nIndex)


func CopyFrom(_pOther):
    self.m_nCharFunctionTimeCount = _pOther.m_nCharFunctionTimeCount
    for i in range(_pOther.m_nCharFunctionTimeCount):
        self.m_CharFunctionTime[i] = SCharFunctionTime.new(self)
        self.m_CharFunctionTime[i].CopyFrom(_pOther.m_CharFunctionTime[i])

func UnPackField(_fieldId, _bufferStream, _varLen):
    var elemCount = self.m_nCharFunctionTimeCount
    if _fieldId < 0 or _fieldId > TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME:
        # var szLog = string.format("CCharFunctionTimeArray.UnPackField Error :\
        #  _fieldId(%d) < 0 or _fieldId(%d) > self.MaxCharFunctionTimeEleCount()(%d)",
        #  _fieldId, _fieldId, TableCommonDefine.MaxRowElem.MAX_COUNT_CHARFUNCTIONTIME)
        return false
    var elem = null
    var old = null
    if self.m_CharFunctionTime[_fieldId] != null:
        elem = self.m_CharFunctionTime[_fieldId]
        old = SCharFunctionTime.new( self )
        old.CopyFrom( elem )
    else:
        elem = SCharFunctionTime.new(self)
        self.m_CharFunctionTime[_fieldId] = elem
        self.m_CharFunctionTime[_fieldId].nIndex = _fieldId

    if elemCount <= _fieldId:
        self.m_nCharFunctionTimeCount = _fieldId + 1

    elem.Update(_bufferStream, _fieldId )
    elem_update.emit(self, old, elem, _nIndex)
    return true

func AddField(_fieldId, _bufferStream):
    var elem = SCharFunctionTime.new(self)
    elem.UnPack(_bufferStream, true )
    elem.nIndex = _fieldId
    if null == self.m_CharFunctionTime[_fieldId]:
        self.m_CharFunctionTime[_fieldId] = elem
    else:
        var uuid = elem.m_UUID;
        for index in range(self.m_nCharFunctionTimeCount, _fieldId+1, -1):
            self.m_CharFunctionTime[index] = self.m_CharFunctionTime[index- 1]
            self.m_CharFunctionTime[index].m_UUID = uuid + index - _fieldId
            self.m_CharFunctionTime[index].nIndex = index
        self.m_CharFunctionTime[_fieldId] = elem;
        self.m_CharFunctionTime[_fieldId].nIndex = _fieldId

    self.m_nCharFunctionTimeCount = self.m_nCharFunctionTimeCount + 1;
    elem_add.emit(self, null, elem, _fieldId)

func DelField(_fieldId ):
    if self.m_CharFunctionTime[_fieldId] == null:
        return 
    
    self.m_nCharFunctionTimeCount = self.m_nCharFunctionTimeCount - 1
    var elem = self.m_CharFunctionTime[_fieldId]
    if _fieldId != self.m_nCharFunctionTimeCount:
        var uuid = self.m_CharFunctionTime[_fieldId].m_UUID;
        self.m_CharFunctionTime[_fieldId] = self.m_CharFunctionTime[self.m_nCharFunctionTimeCount]
        self.m_CharFunctionTime[_fieldId].m_UUID = uuid
        self.m_CharFunctionTime[_fieldId].nIndex = _fieldId

    self.m_CharFunctionTime[self.m_nCharFunctionTimeCount] = null
    elem_del.emit(self, elem, null, _fieldId)   

func Filter( funFilter ):
    var tblItems = []
    for i in range(self.m_nCharFunctionTimeCount-1):
        if funFilter.call(self.m_CharFunctionTime[i], i):
            tblItems.append(self.m_CharFunctionTime[i])
    return tblItems

func QueryByFunctionID(FunctionID):
    var nCnt = self.m_nCharFunctionTimeCount
    for i in range(nCnt-1):
        var curElem = self.m_CharFunctionTime[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_FunctionID == FunctionID :
                return curElem
    return null


